<?php
class PhanxulyController extends AppController {
	public $uses = array('Hoso','Tthc','ThanhPhan','ThanhPho','ThanhPhanTthc','Status','User', 'LichsuHoso');
	public $helpers = array('Status');
	
	public function beforeFilter() {
		$this->layout = 'quanly';
	}
	
	public function index($status = null) {
		$st = array(
			$this->Status->CHO_PHAN_XULY, 
			$this->Status->PHAN_XULY_TU_DONG,
			$this->Status->PHAN_XULY_TRUC_TIEP,
			$this->Status->LAY_HS_TON_DONG);
			
		if (!empty($status) && in_array($status, $st)) {
			$st = array($status);
		}
		
		$listHosos = $this->Hoso->find("all", array(
			'conditions' => array('Hoso.trang_thai' => $st) 
		));
		
		$listStatus = array(
			1 => 'All',
			$this->Status->CHO_PHAN_XULY => 'Chờ phân xử lý', 
			$this->Status->PHAN_XULY_TU_DONG => 'Phân xử lý tự động',
			$this->Status->PHAN_XULY_TRUC_TIEP => 'Phân xử lý trực tiếp',
			$this->Status->LAY_HS_TON_DONG => 'Hồ sơ tồn đọng');
			
		$this->set('listStatus', $listStatus);
		$this->set('status', $status);
		
		$this->set('listHosos', $listHosos);
	}
	
	public function phanCong ($id = null) {
		$hoso = $this->Hoso->findById($id);
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$this->set('thanhPhos', $thanhPhos);
		
		$this->User->recursive = -1;
		
		$users = $this->User->find("list", array("conditions" => array("User.username !=" => 'admin'), 'fields'=>array('id','ho_ten')));		
		// debug($users);
		
		$this->set("hoso", $hoso);
		$this->set("users", $users);
	}
	
	public function phanXuLy ($id = null) {
		if (!empty($this->request->data)) {
			$hoso = $this->Hoso->findById($this->request->data['Hoso']['id']);
			$date1 = new DateTime();
			$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
			
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			
			$this->Hoso->saveField('trang_thai', $this->Status->PHAN_XULY_TRUC_TIEP);
			$this->Hoso->saveField('nguoi_xuly_id', $this->request->data['Hoso']['nguoi_xuly_id']);
			$this->Hoso->saveField('nguoi_kiemduyet_id', $this->request->data['Hoso']['nguoi_kiemduyet_id']);
			$this->Hoso->saveField('nguoi_ky_id', $this->request->data['Hoso']['nguoi_ky_id']);
			$this->Hoso->saveField('nguoi_phanxuly_id', $this->Auth->user("id"));
			$this->Hoso->saveField('nguoi_phanxuly_id', $this->Auth->user("id"));
			
			//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
			
			$this->Hoso->save();
			
			// update history
			
			
			$his = array(); $his['LichsuHoso'] = array();
			
			$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
			$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
			$his['LichsuHoso']['status'] = $this->Status->PHAN_XULY_TRUC_TIEP;
			$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Đã phân xử lý';
			$his['LichsuHoso']['thao_tac'] = 'Phân xử lý';
			$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
			
			$this->LichsuHoso->create();
			$this->LichsuHoso->save($his);
			
			$this->redirect("index");
		}
	}
	
	public function chuyenXuly ($id = null) {
		if (!empty($this->request->params['pass'][0])) {
			$selections = split(",",$this->request->params['pass'][0]."-1");
			foreach ($selections as $key => $id) {
				$this->Hoso->recursive = -1;
				$hoso = $this->Hoso->findById($id);
				if (!empty($hoso) && $hoso['Hoso']['trang_thai'] == $this->Status->PHAN_XULY_TRUC_TIEP) {
					$date1 = new DateTime();
					$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
					
					$this->Hoso->id= $id;
					$this->Hoso->saveField('trang_thai', $this->Status->CHO_XULY);
					//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
					$this->Hoso->save();
					
					// update history
					
					$his = array(); $his['LichsuHoso'] = array();
					$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
					$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
					$his['LichsuHoso']['status'] = $this->Status->CHO_XULY;
					$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Chuyển xử lý';
					$his['LichsuHoso']['thao_tac'] = 'Chờ xử lý';
					$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
					
					$this->LichsuHoso->create();
					$this->LichsuHoso->save($his);
				}
			}
		} else {
			$this->log("Rejecting document: invalid document id= "+$id);
		}
		// $this->render('/index.ctp');
		$this->redirect('index');
	}
	
	public function back () {
		$this->redirect('index');
	}
}
